package com.xinou.daily_report.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinou.common.base.config.ApiResponse;
import com.xinou.common.base.config.JwtModel;
import com.xinou.common.enumerate.TokenTypeEnum;
import com.xinou.common.util.HttpServletRequestUtil;
import com.xinou.common.util.JwtUtil;
import com.xinou.daily_report.expand.sso.mini.MiniRealm;
import com.xinou.sso.entity.SysUser;
import com.xinou.sso.enumerate.HttpSsoStatusEnum;
import com.xinou.sso.po.LoginPO;
import com.xinou.sso.service.SysUserService;
import com.xinou.sso.util.ShiroRedisUtil;
import com.xinou.sso.util.ShiroUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-06-11
 */
@RestController
@RequestMapping("/mini/")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(tags = {"前台登录"})
public class MiniLoginController {

    private final SysUserService sysUserService;
    private final ShiroRedisUtil shiroRedisUtil;
    private final MiniRealm miniRealm;

    @PostMapping("login")
    @ApiOperation(value = "登录")
    public ApiResponse login(@RequestBody LoginPO po) {

        SysUser sysUser = sysUserService.getOne(
                new LambdaQueryWrapper<SysUser>()
                    .eq(SysUser::getAccount, po.getAccount())
        );
        if(sysUser == null) {
            return new ApiResponse(HttpSsoStatusEnum.RE_CODE_NO_HAVE_ACCOUNT);
        }
        String password = new SimpleHash(Md5Hash.ALGORITHM_NAME, po.getPwd(), sysUser.getSalt()).toHex();
        if (!Objects.equals(password, sysUser.getPassword())) {
           return new ApiResponse(HttpSsoStatusEnum.RE_CODE_PASSWORD_ERROR);
        }
        JwtModel jwtModel = JwtModel
                .builder()
                .userId(sysUser.getId())
                .account(sysUser.getAccount())
                .userName(sysUser.getAccount())
                .build();
        return new ApiResponse<>(JwtUtil.getWebJwtString(jwtModel));
    }

    @PostMapping("logout")
    @ApiOperation(value = "登出")
    public ApiResponse logout() {
        String token = HttpServletRequestUtil.getToken(TokenTypeEnum.TYPE_WEB);
        if (StrUtil.isBlank(token)) {
            return new ApiResponse();
        }
        Long userId = ShiroUtil.getUserId();
        shiroRedisUtil.insertBlackList(userId, token, TokenTypeEnum.TYPE_WEB);
        miniRealm.clearAuthenticationInfo();
        miniRealm.clearAuthorizationInfo();
        return new ApiResponse();
    }

}

